package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.market;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.goods.dao.db.Goods;
import com.gzhryc.net_marketing.cloud.goods.dao.enums.GoodsEnum;
import com.gzhryc.net_marketing.cloud.goods.services.GoodsService;
import com.gzhryc.net_marketing.cloud.goods.services.dto.GoodsSearch;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsInfoRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsInfoResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsListRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.market.GoodsListResponse;

public class GoodsController {

    @Route(value = "/agent_market/api/v1/goods/list.data")
    public GoodsListResponse list(HttpServletRequest request){
        GoodsListResponse apiResp = new GoodsListResponse(ClientAPICodeConstants.NULL_DATA,"暂无数据");
        GoodsListRequest apiReq = WebUtils.getJsonRequest(request,GoodsListRequest.class);
        if(apiReq != null) {
            Integer pageNo = apiReq.getBody().getPageNo() == null ? 1 : apiReq.getBody().getPageNo();
            Integer pageLimit = apiReq.getBody().getPageLimit() == null ? WebConstants.PAGE_LIMIT:apiReq.getBody().getPageLimit();

            GoodsSearch search = new GoodsSearch();
            search.setState(GoodsEnum.State.PutOnTheShelf.index());
            List<Goods> goodsList = GoodsService.self(MarketConstants.JDBC_KEY).findBySearch(search,pageNo, pageLimit);
            if(goodsList != null && goodsList.size() > 0) {
                apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                apiResp.getBody().setItems(new ArrayList<>());
                for(Goods goods : goodsList){
                    GoodsListResponse.Item item = new GoodsListResponse.Item();
                    item.setId(goods.getId());
                    item.setName(goods.getName());
                    item.setLabels(goods.getLabels());
                    item.setThumbnailImg(goods.getThumbnailImg());
                    item.setPayMoneyStr(goods.getPayMoneyStr());
                    apiResp.getBody().getItems().add(item);
                }
            }
        }
        return apiResp;
    }

    @Route(value = "/agent_market/api/v1/goods/info.data")
    public GoodsInfoResponse info(HttpServletRequest request){
        GoodsInfoResponse apiResp = new GoodsInfoResponse(ClientAPICodeConstants.NULL_DATA,"暂无数据");
        GoodsInfoRequest apiReq = WebUtils.getJsonRequest(request,GoodsInfoRequest.class);
        if(apiReq != null && apiReq.getBody().getGoodsId() != null) {
            Goods goods = GoodsService.self().getById(apiReq.getBody().getGoodsId());
            if(goods != null){
                apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);

                apiResp.getBody().setId(goods.getId());
                apiResp.getBody().setName(goods.getName());
                apiResp.getBody().setThumbnailImg(goods.getThumbnailImg());
                apiResp.getBody().setImgs(goods.getImgs());
                apiResp.getBody().setDetailImgs(goods.getDetailImgs());
                apiResp.getBody().setLabels(goods.getLabels());
                apiResp.getBody().setPayMoneyStr(goods.getPayMoneyStr());
                apiResp.getBody().setState(goods.getState());
                apiResp.getBody().setStateName(goods.getStateName());
            }
        }
        return apiResp;
    }
}
